package com.tchintech.pay.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.model.entity.PayDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/9 0009.
 */

public class PayDetailAdapter extends BaseAdapter {
    private List<PayDetail> list;
    private Context context;

    public PayDetailAdapter(Context context, List<PayDetail> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<PayDetail> list) {
        if (list != null) {
            if (!this.list.containsAll(list)) {
                this.list.addAll(list);
            }
        } else {
            this.list = new ArrayList<PayDetail>();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public PayDetail getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        Viewholde vh = null;
        if (v == null) {
            vh = new Viewholde();
            v = LayoutInflater.from(context).inflate(R.layout.adapter_detail, null);
            vh.time = (TextView) v.findViewById(R.id.tv_time);
            vh.name = (TextView) v.findViewById(R.id.tv_name);
            vh.state = (TextView) v.findViewById(R.id.tv_state);
            vh.money = (TextView) v.findViewById(R.id.tv_money);
            v.setTag(vh);
        } else {
            vh = (Viewholde) v.getTag();
        }
        PayDetail payDate = list.get(position);
        if (payDate.getDes().equals("支付成功")) {
            vh.state.setText(payDate.getDes());
            vh.state.setTextColor(ContextCompat.getColor(context, R.color.green_success));
        } else {
            vh.state.setText("支付失败");
            vh.state.setTextColor(ContextCompat.getColor(context, R.color.red_fail));
        }
        vh.time.setText(payDate.getTransaction_time());
        vh.money.setText(payDate.getPay_amount() + "元");
        vh.name.setText(payDate.getTransaction_type());
        return v;
    }

    class Viewholde {
        TextView time, name, state, money;

    }
}
